/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */
package space.app.meshview;

import java.awt.Cursor;
import java.net.URL;
import javax.media.opengl.awt.GLCanvas;
import space.app.common.AbstractRender;
import space.app.common.ErrorReportEventListener;
import space.app.common.JOGLPanel;
import space.app.meshview.state.ViewPointStateMachine;
import space.game.ShaderProgram;

/**
 *
 * @author Pierre
 */
public class MeshRendrer extends AbstractRender<MeshEventListner> {

    public MeshRendrer(final JOGLPanel panel, final ShaderProgram shaderProgram) {
        super(new MeshEventListner(), panel);
        if (shaderProgram == null) {
            throw new IllegalArgumentException();
        }
        eventListner.useShader(shaderProgram);
    }

    @Override
    protected void buildCanvas() {
        final ViewPointStateMachine vpm = new ViewPointStateMachine(eventListner.camera, eventListner.lightDirection);
        final GLCanvas canvas = new GLCanvas();
        canvas.addMouseListener(vpm);
        canvas.addMouseMotionListener(vpm);
        canvas.addMouseWheelListener(vpm);
        canvas.setCursor(new Cursor(Cursor.MOVE_CURSOR));
        canvas.addGLEventListener(new ErrorReportEventListener(eventListner));
        panel.setCanvas(canvas);
    }

    void load(URL url) {
        eventListner.load(url);
        panel.display();
    }

    void useShader(ShaderProgram shaderProgram) {
        if (shaderProgram == null) {
            throw new IllegalArgumentException();
        }
        eventListner.useShader(shaderProgram);
        panel.display();
    }

    void setFBOVisible(final boolean selected) {
        eventListner.setFBOVisible(selected);
        panel.display();
    }

    void setTBNVisible(final boolean selected) {
        eventListner.setTBNVisible(selected);
        panel.display();
    }
}
